﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using 立库托盘入库.bean;
using 立库托盘入库.ViewModel;

namespace 立库托盘入库
{
    /// <summary>
    /// ChacheUsername.xaml 的交互逻辑
    /// </summary>
    public partial class ChacheUsernameDialog : UserControl
    {
        MainViewModel _viewModel;
        String path = "";
        public ChacheUsernameDialog()
        {
            InitializeComponent();
            path = AppDomain.CurrentDomain.BaseDirectory;
            _viewModel = MainViewModel.MainViewModelCreate();
            base.DataContext = _viewModel;
            _viewModel.getUserData("");
        }

        public void writXUserName()
        {
            string filePath = path + "save\\XUserName.xml";
            XElement datas = new XElement("UserData");
            foreach (UserBean bean in _viewModel.XUserBeans)
            {
                XElement data = new XElement("data");
                data.Add(new XElement("UserName", bean.UserName));
                data.Add(new XElement("Uid", bean.Uid));
                datas.Add(data);
            }
            datas.Save(filePath);
            List<string> ktls = new List<String>(); ;
            foreach (UserBean bean in _viewModel.XUserBeans)
            {
                ktls.Add(bean.UserName);
            }
            _viewModel.UserNames = ktls.ToArray();
        }

        private void userSouSuo(object sender, RoutedEventArgs e)
        {
            _viewModel.getUserData(UserName.Text);
        }

        private void userTianjia(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var bean = btn.DataContext as UserBean;
            if (!bean.IsXuan)
            {
                _viewModel.isXuanUser(bean, true);
                writXUserName();
            }
        }

        private void userShanchu(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            var bean = btn.DataContext as UserBean;
            //if (bean.IsXuan)
            //{
                _viewModel.isXuanUser(bean, false);
                writXUserName();
            //}
        }

        private void closeUser(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("sssssssssssss");
            _viewModel.isChacheUsername = Visibility.Hidden;
        }
        

    }
}
 